#include "modules/AssistModule/ParamLoadModule.h"
#include "modules/AssistModule/LogModule.h"
#include "data_struct/base/LogBase.h"
#include <boost/assign/list_of.hpp>
#include "modules/AssistModule/TextLog.h"

namespace behavior_controller
{
const std::map<std::string, LogLevel> severity_map =
        boost::assign::map_list_of<std::string, LogLevel>(
                std::string("debug"), LogLevel::debug)(
                std::string("info"), LogLevel::info)(
                std::string("warn"), LogLevel::warn)(
                std::string("error"), LogLevel::error);


void ParamLoadModule::LoadLogYaml()
{
    std::stringstream file_pre;
    std::string log_yaml_path, contour_yaml_path, parameters_path;
    LogContent content;
    file_pre << getenv("HOME") << "/ros2_ws/src/Sweeping_robot/behavior_controller/conf/";
    log_yaml_path = file_pre.str() + "log.yaml";
    contour_yaml_path = file_pre.str() + "contours.yaml";
    parameters_path = file_pre.str() + "parameters.yaml";

    log_yaml_ = YAML::LoadFile(log_yaml_path);
    parameters_yaml_ = YAML::LoadFile(parameters_path);
    if (parameters_yaml_["behavior"].IsDefined())
    {
        if (parameters_yaml_["behavior"]["sim_mode"].IsDefined())
        {
            if (parameters_yaml_["behavior"]["sim_mode"].as<int>() == 1)
                contour_yaml_path = file_pre.str() + "contours_sim.yaml";
        }
    }
    contour_yaml_ = YAML::LoadFile(contour_yaml_path);

    if (!log_yaml_["logs"].IsDefined())
    {
        LogPub::Info("behavior_controller", "[LoadLogYaml] Is not defined");
        return;
    }

    if (log_yaml_["logs"]["name"].IsDefined())
    {
        content.name = log_yaml_["logs"]["name"].as<std::string>();
        LogPub::Info("behavior_controller", "[LoadLogYaml] Name is:", content.name);
    }
    else
    {
        LogPub::Info("behavior_controller", "[LoadLogYaml] Name is not defined");
    }

    if (log_yaml_["logs"]["severity"].IsDefined())
    {
        content.severity = severity_map.at(log_yaml_["logs"]["severity"].as<std::string>());
    }
    else
    {
        LogPub::Info("behavior_controller", "[LoadLogYaml] severity is not defined");
    }

    if (log_yaml_["logs"]["directory"].IsDefined())
    {
        content.directory = log_yaml_["logs"]["directory"].as<std::string>();
    }
    else
    {
        LogPub::Info("behavior_controller", "[LoadLogYaml] directory is not defined");
    }

    if (log_yaml_["logs"]["storage-directory"].IsDefined())
    {
        content.storage_directory = log_yaml_["logs"]["storage-directory"].as<std::string>();
    }
    else
    {
        LogPub::Info("behavior_controller", "[LoadLogYaml] storage-directory is not defined");
    }

    LogPub::SetLog<TextLog>(content.name, content.severity, content.directory, content.storage_directory);
}

} // namespace behavior_controller